﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twitter;
using FriendsNearby.Services;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace FriendsNearbyBot
{
    class NearbyNotifierProcessor : IMessageProcessor
    {
        IMessageBuilder messageBuilder;
        IUsersService userService;
        ILocationService locationService;
        IPeopleNearbyService peopleService;


        public NearbyNotifierProcessor(IUsersService userService, ILocationService locationService, IPeopleNearbyService peopleService)
            : this(userService, locationService, peopleService, new ListMessageBuilder())
        {
        }

        public NearbyNotifierProcessor(IUsersService userService, ILocationService locationService, IPeopleNearbyService peopleService, IMessageBuilder messageBuilder)
        {
            this.userService = userService;
            this.locationService = locationService;
            this.peopleService = peopleService;
            this.messageBuilder = messageBuilder;
        }

        public bool CanProcess(Message message)
        {
            return message.Text == "new";
        }

        public RunNextProcessor Process(IBotClient client, Message message)
        {
            if (CanProcess(message))
            {
				UserRegistry user = userService.QueryUserRegistry( x =>x.TwitterScreenName == message.Sender.ScreenName);

                messageBuilder.Clear();

                if (user == null)
                {
                    // Warn the user via SMS that the mapping has not been established.
                    messageBuilder.AppendFragment(String.Format(Properties.Resources.TwitterNotMapped,
                        message.Sender.ScreenName));
                    client.SendMessages(message.Sender.ScreenName, messageBuilder.GetMessages(), message, this.GetType().Name);
                }
                else
                {
					Logger.Write(string.Format("Getting notifications for userID:{2}, facebookID:{0},twitter ScreenName: {1}", user.FacebookUserId, message.Sender.ScreenName, user.UserID));
					var notifications = peopleService.GetNotificationsForUser(user.UserID);
                    int numberOfNotifications = notifications.Count;
                    Logger.Write("{0} notifications found", numberOfNotifications.ToString());

                    if (numberOfNotifications == 0)
                        messageBuilder.AppendFragment("No new nearby friends.");
                    else if (numberOfNotifications == 1)
                        messageBuilder.AppendFragment(string.Format("{0} is now nearby.", notifications.First()));
                    else
                    {
                        int i = 0;
                        while(i < numberOfNotifications - 2)
                        {                            
                            messageBuilder.AppendFragment(string.Format("{0}, ", notifications[i]));
                            i++;
                        }
                        messageBuilder.AppendFragment(string.Format("{0} and {1} are now nearby.", notifications[i], notifications[i + 1]));
                    }

                    client.SendMessages(message.Sender.ScreenName, messageBuilder.GetMessages(), message, this.GetType().Name);

                    // TODO: Add try/catch.
					locationService.SaveLastNotificationDate(user.UserID, DateTime.Now);

                }

                BotProcessor.MessageProcessed(client, message);

                return RunNextProcessor.No;
            }
            else
                return RunNextProcessor.Yes;

        }
    }
}
